package com.dingqb.androidproject.excel;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import jxl.Sheet;
import jxl.Workbook;

/**
 * Created by dingqb on 2018/5/14.
 */

public class ExceLActivity extends Activity {
    String TAG = "ExceLActivity";
    ArrayList<ExcelModel> excelModels = new ArrayList<>();
    List<XmlModel> xmlModelList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        File file = new File("./output_data");
        file.mkdir();

        String result = "";
        File[] files = new File("./").listFiles();
        for (File f : files) {
            Log.e(TAG, "onCreate: name==" + f.getName());
        }

        //        new ExcelDataLoader().execute("aaa.xls", "0");
//        excelModels = getXlsData("aaa.xls", 0);
//        xmlModelList = xml();
//        for (int i = 0; i < xml().size(); i++) {
//            for (int j = 0; j < excelModels.size(); j++) {
//                XmlModel xmlModel = xmlModelList.get(i);
//                ExcelModel excelMode = excelModels.get(j);
//                if (xmlModel.getId().equals(excelMode.getId())) {
//                    Log.e(TAG, "onCreate: id====" + xmlModel.getId() + ", english===" + excelMode.getEnglishName());
//                }
//            }
//        }
    }

    /**
     * 获取 excel 表格中的数据,不能在主线程中调用
     *
     * @param xlsName excel 表格的名称
     * @param index   第几张表格中的数据
     */
    private ArrayList<ExcelModel> getXlsData(String xlsName, int index) {
        ArrayList<ExcelModel> countryList = new ArrayList<ExcelModel>();
        AssetManager assetManager = getAssets();

        try {
            Workbook workbook = Workbook.getWorkbook(assetManager.open(xlsName));
            Sheet sheet = workbook.getSheet(index);
            int sheetNum = workbook.getNumberOfSheets();
            int sheetRows = sheet.getRows();
            int sheetColumns = sheet.getColumns();
            for (int i = 0; i < sheetRows; i++) {
                ExcelModel excelModel = new ExcelModel();
                excelModel.setChinaName(sheet.getCell(0, i).getContents());
                excelModel.setEnglishName(sheet.getCell(1, i).getContents());
                excelModel.setId(sheet.getCell(2, i).getContents());
                countryList.add(excelModel);
            }

            workbook.close();

        } catch (Exception e) {
            Log.e(TAG, "read error=" + e, e);
        }

        return countryList;
    }

    //在异步方法中 调用
    private class ExcelDataLoader extends AsyncTask<String, Void, ArrayList<ExcelModel>> {

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected ArrayList<ExcelModel> doInBackground(String... params) {
            return getXlsData(params[0], 0);
        }

        @Override
        protected void onPostExecute(ArrayList<ExcelModel> excelModels) {

            if (excelModels != null && excelModels.size() > 0) {
                //存在数据
            } else {
                //加载失败
            }

        }
    }

    private List<XmlModel> xml() {
        List<XmlModel> xmlModelList = new ArrayList<>();
        //创建DocumentBuilderFactory对象
        DocumentBuilderFactory a = DocumentBuilderFactory.newInstance();
        try {
            //通过DocumentBuilder对象的parse方法返回一个Document对象
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            builder = builderFactory.newDocumentBuilder();
            Document document = builder.parse(getAssets().open("strings.xml"));
            //通过Document对象的getElementsByTagName()返根节点的一个list集合
            NodeList stringklist = document.getElementsByTagName("string");
            for (int i = 0; i < stringklist.getLength(); i++) {
                //循环遍历获取每一个string
                Node string = stringklist.item(i);
                //通过Node对象的getAttributes()方法获取全的属性值
                NamedNodeMap stringmap = string.getAttributes();
                String context = string.getTextContent();//
                //循环遍每一个string的属性值
                for (int j = 0; j < stringmap.getLength(); j++) {
                    Node node = stringmap.item(j);
                    //通过Node对象的getNodeName()和getNodeValue()方法获取属性名和属性值
                    String value = node.getNodeValue() + "";
                    XmlModel xmlModel = new XmlModel();
                    xmlModel.setId(value);
                    xmlModel.setValue(context);
                    xmlModelList.add(xmlModel);
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return xmlModelList;
    }
}
